class ApplicantController < ApplicationController
  before_filter :user_authorize
  before_filter :sys_before_log
  before_filter :privilege_filter,:except=>[:editApplicantInformation,:applicantList,:newApplicant,:editApplicant,:password]

  def applicantInformation

  end

  def applicantList

  end

  def applicantListForApplicant

  end

  def editApplicant

  end

  def editApplicantInformation

  end

  def newApplicant

  end

  def password

  end

  def passwordForApplicant
    
  end

  #得到申购人列表
  def getApplicants
    #是否专为表单准备？如果只是为销售订单而非编辑Customer本身准备的就不传过多的信息
    forCBB = params[:for_cbb]
    if forCBB != 'true'
      forCBB = 'false'
    end
    start = params[:start]
    limit = params[:limit]
    if params[:query].nil?
      queryStr = ""
    else
      queryStr = params[:query]
    end
    applicants = Applicant.name_number_like queryStr,forCBB
    if start.nil?
      applicantsNow = applicants
    else
      applicantsNow = applicants.paginate start,limit
    end

    if forCBB == 'true'
      jsonStr="{totalProperty:"+applicants.length.to_s+",root:"+applicantsNow.to_json()+"}"
    else
      jsonStr="{totalProperty:"+applicants.length.to_s+",root:"+applicantsNow.to_json(:include=>[:department,:role])+"}"
    end
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #
  def getMyInformation
    applicant = Applicant.find(session[:applicant_id])
    jsonstr="{totalProperty:1,root:"+applicant.to_json(:include => [:department])+"}"
    #logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  #启用申购人
  def applyPass
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        applicant = Applicant.find(id)
        applicant.auditing_status = '启用'
        applicant.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #禁用申购人
  def stopApplicant
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        applicant = Applicant.find(id)
        applicant.auditing_status = '禁用'
        applicant.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #添加申购人信息
  def create
    begin
      applicant = Applicant.findByNumber(params[:login_id])
      if applicant==nil
        applicant = Applicant.new
        applicant.login_id = params[:login_id]
        applicant.name = params[:name]
        applicant.phone = params[:phone]
        applicant.mobile = params[:mobile]
        applicant.email = params[:email]
        applicant.department_id = params[:department_id]
        applicant.role_id = params[:role_id]
        applicant.password = params[:login_id]
        applicant.auditing_status = '启用'
        applicant.save!
        info = 'success'
      else
        info = '该编号已被使用，请更换编号'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #获得指定ID的申购人信息
  def edit
    if session[:auditor_id] != nil
      applicant = Applicant.find(params[:id])
    elsif session[:applicant_id] != nil
      applicant = Applicant.find(session[:applicant_id])
    end
    render :text => applicant.to_json(),:layout => false
  end

  #审批人更新申购人信息
  def update
    begin
      applicant = Applicant.findByNumber(params[:login_id])
      if Applicant.find(params[:id]).login_id!=params[:login_id]&&applicant!=nil
        info = '该编号已被使用，请更换编号'
      else
        applicant = Applicant.find(params[:id])
        applicant.login_id = params[:login_id]
        applicant.name = params[:name]
        applicant.email = params[:email]
        applicant.phone = params[:phone]
        applicant.mobile = params[:mobile]
        applicant.department_id = params[:department_id]
        applicant.role_id = params[:role_id]
        applicant.save!
        info = 'success'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #更新自己的信息
  def updateInformation
    begin
      applicant  = Applicant.find(session[:applicant_id])
      applicant.name = params[:name]
      applicant.email = params[:email]
      applicant.phone = params[:phone]
      applicant.mobile = params[:mobile]
      applicant.save!
      info = 'success'
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #申购人修改自己的密码
  def changePassword
    begin
      applicant = Applicant.find(session[:applicant_id])
      if applicant.password == params[:password]
        applicant.password = params[:password1]
        applicant.save!
        info = 'success'
      else
        info = '原密码不正确，请确认'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #审批人修改密码
  def changePasswordForApplicant
    begin
      applicant = Applicant.find(params[:id])
      applicant.password = params[:password1]
      applicant.save!
      info = 'success'
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result,:layout=>false
  end

end
